package com.heima.homeservicemanagementsystem.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.heima.homeservicemanagementsystem.entity.WxUser;
import com.heima.homeservicemanagementsystem.service.WxUserService;

import java.util.List;
import java.util.Objects;

/**
 * 微信用户表;(wx_user)表控制层
 *
 * @author : http://www.chiner.pro
 * @date : 2025-3-27
 */
@Slf4j
@Tag(name = "微信用户表对象功能接口")
@RestController
@RequestMapping("/wxUser")
public class WxUserController {

    @Resource
    private WxUserService wxUserService;

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Result<Page<WxUser>> paginQuery(@RequestBody PageParams<WxUser> pageParams) {
        //查询参数
        WxUser params = pageParams.getParams();
        LambdaQueryWrapper<WxUser> lqw = new LambdaQueryWrapper<>();
        return Result.success(wxUserService.page(pageParams.getPage(), lqw));
    }

    @Operation(summary = "微信用户所有搜索")
    @GetMapping("/search")
    public Result searchQuery() {
        List<WxUser> list = wxUserService.list();
        return Result.success(list);
    }

    @Operation(summary = "有条件的搜索")
    @PostMapping("/tjSearch")
    public Result Query(@RequestBody WxUser wxUser,
                        @RequestParam(value = "startTime", required = false) String startTime,
                        @RequestParam(value = "endTime", required = false) String endTime) {
        log.info("用户信息：{}", wxUser.getId());
        log.info("开始：{}", startTime);
        log.info("结束：{}", endTime);

        LambdaQueryWrapper<WxUser> lqw = new LambdaQueryWrapper<>();

        // 其他查询条件
        lqw.eq(wxUser.getId() != null, WxUser::getId, wxUser.getId());

        lqw.like(wxUser.getUsername() != null, WxUser::getUsername, wxUser.getUsername());
        lqw.eq(wxUser.getPhone() != null, WxUser::getPhone, wxUser.getPhone());
        lqw.eq(wxUser.getEmail() != null, WxUser::getEmail, wxUser.getEmail());
//        lqw.eq(wxUser.getBirthday() != null, WxUser::getBirthday, wxUser.getBirthday());
        lqw.eq(wxUser.getSex() != null, WxUser::getSex, wxUser.getSex());
        lqw.eq(wxUser.getIdentityCard() != null, WxUser::getIdentityCard, wxUser.getIdentityCard());
        lqw.eq(wxUser.getStatus() != null, WxUser::getStatus, wxUser.getStatus());
        // 如果有时间段查询条件
        if (StringUtils.hasLength(startTime) && StringUtils.hasLength(endTime)) {
            lqw.between(WxUser::getBirthday, startTime, endTime);
        } else if (StringUtils.hasLength(startTime)) {
            lqw.ge(WxUser::getBirthday, startTime);  // 大于等于startTime
        } else if (StringUtils.hasLength(endTime)) {
            lqw.le(WxUser::getBirthday, endTime);  // 小于等于endTime
        }

        return Result.success(wxUserService.list(lqw));
    }

    @Operation(summary = "获取微信用户信息")
    @GetMapping("/wxUsersearch")
    public Result getwxUser() {

//        LambdaQueryWrapper<WxUser> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(WxUser::getOpenid, wxUser.getOpenid());
//        lqw.eq(WxUser::getId, wxUser.getId());

        WxUser byId = wxUserService.getById(StpUtil.getLoginIdAsString());

        return Result.success(byId);
    }

    /**
     * 新增数据
     *
     * @param wxUser 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增数据")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody WxUser wxUser) {
        if (wxUserService.save(wxUser)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param wxUser 实例对象
     * @return 实例对象
     */
    @Operation(summary = "更新数据")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody WxUser wxUser) {
        WxUser byId = wxUserService.getById(wxUser.getId());
//            WxUser wxUser1 = new WxUser();
        if (Objects.nonNull(byId)) {
            log.info("修改用户信息:{}", wxUser);
            if (StringUtils.hasLength(wxUser.getIdentityCard()) && wxUser.getIdentityCard().contains("*")) {
                wxUser.setIdentityCard(byId.getIdentityCard());
                log.info("身份证号：" + wxUser.getIdentityCard());
                if (wxUserService.updateById(wxUser)) {
                    return Result.success();
                }
            } else {
                if (wxUserService.updateById(wxUser)) {
                    return Result.success();
                }
            }
        }


        return Result.error("未找到该用户");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据",
            parameters = {
                    @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
            })
    @GetMapping("/delete/{id}")
    public Result<Boolean> deleteById(@PathVariable String id) {

        if (wxUserService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "多id删除数据")
    @PostMapping("/delete")
    public Result<Boolean> deleteByIds(@RequestBody List<String> id) {
        log.info("前端信息：{}", id);
        if (wxUserService.removeByIds(id)) {
            return Result.success();
        }
        return Result.error();
    }

}